package Control;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.itextpdf.text.DocumentException;

import Model.ConnectDatabase;
import Model.GioHang;
import Model.InfoPayment;
import Model.PDFCreater;
import Model.ThongTinNguoiNhan;
import Model.ThongTinUser;
import Model.User;

/**
 * Servlet implementation class InfoCheckOut
 */
public class InfoCheckOut extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public InfoCheckOut() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		 response.setContentType("text/html;charset=UTF-8");
	        HttpSession session = request.getSession();        
	        request.setCharacterEncoding("UTF-8");
	        response.setCharacterEncoding("UTF-8");
	        
	        String correctAnswer = (String)session.getAttribute("CorrectAnswer");
	        String provinceID = request.getParameter("province");
	      int province = Integer.parseInt(provinceID);
	        String provinceName = "";
	        Connection con = ConnectDatabase.getConnection();
	        try {
	            PreparedStatement sta = con.prepareStatement("select name from province where provinceid=?");
	            sta.setInt(1, Integer.parseInt(provinceID));
	            ResultSet res = sta.executeQuery();
	            res.next();
	            provinceName = res.getString(1);
	            con.close();
	        } catch (Exception e) {
	            e.printStackTrace();
	        }
	        String captcha = request.getParameter("captcha");
	        if (captcha.equals(correctAnswer)) {
	            String name = request.getParameter("txt_name");
	            String phone = request.getParameter("txt_phone");
	    //        String IDCARD = request.getParameter("txt_cmnd");
	            String cmnd = request.getParameter("txt_cmnd");
	            String address = request.getParameter("address");
	            String note = request.getParameter("note");
	            String paytype = request.getParameter("paytype");
	            
	          //  ThongTinNguoiNhan thongTinNguoiNhan= new ThongTinNguoiNhan(idCard, cMND, ghiChu);
	      //      InfoPayment pay = new InfoPayment(name, phone, IDCARD, provinceID, provinceName, address, note, paytype);
	            ThongTinUser thongTinUser= new ThongTinUser(name, phone, province, address);
	            ThongTinNguoiNhan thongTinNguoiNhan = new ThongTinNguoiNhan(cmnd, note, thongTinUser);
	            
	            session.setAttribute("pay", thongTinNguoiNhan);
	            GioHang cart = (GioHang)session.getAttribute("cart");
	            try {
					User user = (User) session.getAttribute("sessionlogin");
	                String filedownload = user.getUsername() + "_" +System.currentTimeMillis() + ".pdf";
//	                String filedownload = "/Temp/" ;
	                
	          //      PDFCreater.createPDF(getServletContext().getRealPath("/") + filedownload ,thongTinNguoiNhan , cart);
	                PDFCreater.createPDF("D:/Temp/" + filedownload ,thongTinNguoiNhan , cart);
	                session.setAttribute("filename", filedownload);
	            } catch (FileNotFoundException ex) {
	                Logger.getLogger(InfoCheckOut.class.getName()).log(Level.SEVERE, null, ex);
	            } catch (DocumentException ex) {
	                Logger.getLogger(InfoCheckOut.class.getName()).log(Level.SEVERE, null, ex);
	            }
	            response.sendRedirect("summary.jsp");
	   //         response.sendRedirect("startpayment.jsp");
	         //   RequestDispatcher dispatcher = request.getRequestDispatcher("/checkout.jsp");
	         //   dispatcher.forward(request, response);
	        } else {
	            request.setAttribute("captchaerror", "Nhập lại mã captcha");
	            RequestDispatcher dispatcher = request.getRequestDispatcher("/checkout.jsp");
	            dispatcher.forward(request, response);
	        }
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
